package com.grouponmall.service;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.LinkStringFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.grouponmall.model.Deal;

/**
 * @author xueqiang.mi
 * @since 2010-7-17
 */
public class RetrieveFromTippr extends BaseRetrieveService {

  public static final String URL = "http://tippr.com/";
  public static final String SOURCE = "Tippr";

  @Override
  public List<Deal> extract(String link) throws Exception {
    List<Deal> deals = new ArrayList<Deal>();

    Parser parser = new Parser(link);
    NodeList nodeList = parser
      .extractAllNodesThatMatch(new OrFilter(new HasAttributeFilter("id", "channel-selector"), new AndFilter(
        new LinkStringFilter("/"), new HasParentFilter(new HasAttributeFilter("class", "snapshot-image")))));
    String city = "";
    Div div = (Div) nodeList.elementAt(0);
    NodeList temp = div.getChildren().extractAllNodesThatMatch(
      new AndFilter(new LinkStringFilter("/"), new HasParentFilter(new TagNameFilter("h2"))), true);
    if (temp.size() == 1) {
      city = temp.elementAt(0).getFirstChild().getText().trim();
      city = city.substring(0, city.indexOf(" "));
    }

    nodeList.remove(0);
    for (Node node : nodeList.toNodeArray()) {
      LinkTag lt = (LinkTag) node;

      Deal deal = new Deal();
      deal.setCity(city);
      deal.setSource(SOURCE);
      deal.setLink(lt.getLink());

      parser = new Parser(lt.getLink());
      temp = parser.extractAllNodesThatMatch(new HasAttributeFilter("id", "offer-view"));
      NodeList nodeInfo = temp.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("h2"), new HasParentFilter(
        new HasAttributeFilter("id", "offer-purchase"))), true);
      if (nodeInfo.size() == 1) {
        deal.setTitle(nodeInfo.elementAt(0).getFirstChild().getText().trim());
      }

      nodeInfo = temp.extractAllNodesThatMatch(new TagNameFilter("dd"), true);
      if (nodeInfo.size() == 3) {
        deal.setRetailValue(nodeInfo.elementAt(0).getFirstChild().getText().trim());
        String discount = nodeInfo.elementAt(1).getFirstChild().getText().trim();
        discount = discount.substring(0, discount.indexOf(" "));
        deal.setDiscount(discount);
      }

      nodeInfo = temp.extractAllNodesThatMatch(new HasAttributeFilter("id", "cost"), true);
      if (nodeInfo.size() == 1) {
        Node n = nodeInfo.elementAt(0);
        String price = n.getFirstChild().getFirstChild().getText().trim();
        price += n.getChildren().elementAt(1).getText().trim();
        deal.setPrice("$" + price);
      }

      nodeInfo = temp.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("img"), new HasParentFilter(
        new HasAttributeFilter("id", "offer-banner"))), true);
      if (nodeInfo.size() == 1) {
        ImageTag image = (ImageTag) nodeInfo.elementAt(0);
        deal.setImage(image.getAttribute("src"));
      }
      deals.add(deal);
    }
    return deals;
  }

  @Override
  public NodeList getExpectedLinks(String link) throws ParserException {
    Parser parser = new Parser(link);
    NodeList nodeList = parser.extractAllNodesThatMatch(new HasAttributeFilter("id", "channel-selector"));
    if (nodeList.size() == 1) {
      nodeList = nodeList.extractAllNodesThatMatch(new AndFilter(new LinkStringFilter("/"), new HasParentFilter(
        new TagNameFilter("li"))), true);
    }
    return nodeList;
  }

  @Override
  public String getURL() {
    return URL;
  }

}
